package com.coolw.data.migrate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.coolw.data.migrate.common.alarm.AlarmStatusEnum;
import com.coolw.data.migrate.enums.MigrateStatusEnum;
import com.coolw.data.migrate.entity.DataMigrateLogEntity;
import com.coolw.data.migrate.mapper.DataMigrateLogMapper;
import com.coolw.data.migrate.service.DataMigrateLogService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 数据迁移日志Service接口实现类
 *
 * @author coolw
 * @date 2024/2/21 10:21
 */
@Service
public class DataMigrateLogServiceImpl extends ServiceImpl<DataMigrateLogMapper, DataMigrateLogEntity>
        implements DataMigrateLogService {

    @Override
    public List<DataMigrateLogEntity> scanNeedAlarmList() {
        LambdaQueryWrapper<DataMigrateLogEntity> queryWrapper = Wrappers.<DataMigrateLogEntity>lambdaQuery()
                .ne(DataMigrateLogEntity::getStatus, MigrateStatusEnum.SUCCESS.getCode())
                .in(DataMigrateLogEntity::getAlarmStatus, AlarmStatusEnum.needAlarmStatus())
                .lt(DataMigrateLogEntity::getAlarmCount, 3)
                .orderByAsc(DataMigrateLogEntity::getId);
        return list(queryWrapper);
    }

    @Override
    public boolean updateAlarmStatusAndCount(int id, AlarmStatusEnum alarmStatusEnum, int alarmCount) {
        LambdaUpdateWrapper<DataMigrateLogEntity> updateWrapper = Wrappers.<DataMigrateLogEntity>lambdaUpdate()
                .set(DataMigrateLogEntity::getAlarmStatus, alarmStatusEnum.getCode())
                .set(DataMigrateLogEntity::getAlarmCount, alarmCount)
                .eq(DataMigrateLogEntity::getId, id);
        return update(updateWrapper);
    }
}
